import os
import json
from flask import Flask, render_template
from main import Msg

app = Flask(__name__)


@app.route(r'/restart_worker')
def restart_worker():
    os.system("supervisorctl restart qq")
    return render_template("qrcode.html")


@app.route(r"/groups/<int:group_id>/<int:offset>/<int:limit>")
def groups(group_id, offset, limit):

    if not limit:
        limit = 100

    if not offset:
        offset = 0

    q = Msg.select().where(Msg.group == group_id).limit(limit).offset(offset)
    msgs = []
    for msg in q:
        msgs.append({"group": msg.group,
                     "content": msg.content})

    return json.dumps(msgs)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=6000, debug=True)
